
import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '../../config/database';
import User from './User';
import Room from './Room';
import Gift from './Gift';
import LiveSession from './LiveSession';
import UserSnapshot from './UserSnapshot';

// 用户房间礼物关系接口
interface UserRoomGiftAttributes {
  id: string;
  userId: string;
  roomId: string;
  giftId?: string | null; // 关联的礼物ID
  quantity: number; // 礼物数量
  sessionId?: string | null; // 关联的直播场次
  userSnapshotId?: string | null; // 关联的用户快照
  createdAt?: Date;
}

// 创建时可选的属性
interface UserRoomGiftCreationAttributes extends Optional<UserRoomGiftAttributes, 'id' | 'createdAt'> {}

// 用户房间礼物关系模型类
class UserRoomGift extends Model<UserRoomGiftAttributes, UserRoomGiftCreationAttributes> implements UserRoomGiftAttributes {
  declare id: string;
  declare userId: string;
  declare roomId: string;
  declare giftId: string | null;
  declare quantity: number;
  declare sessionId: string | null;
  declare userSnapshotId: string | null;
  
  // 时间戳
  declare readonly createdAt: Date;
}

// 初始化模型
UserRoomGift.init(
  {
    id: {
      type: DataTypes.STRING,
      primaryKey: true,
      allowNull: false
    },
    userId: {
      type: DataTypes.STRING,
      allowNull: false
    },
    roomId: {
      type: DataTypes.STRING,
      allowNull: false
    },
    giftId: {
      type: DataTypes.STRING,
      allowNull: true
    },
    quantity: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
      validate: {
        min: 1
      }
    },

    sessionId: {
      type: DataTypes.STRING,
      allowNull: true
    },
    userSnapshotId: {
       type: DataTypes.UUID,
       allowNull: true
     }
  },
  {
    sequelize,
    tableName: 'user_room_gifts',
    modelName: 'UserRoomGift',
    timestamps: true,
    updatedAt: false, // 礼物记录不需要更新时间
    indexes: [
      {
        fields: ['userId', 'roomId']
      },
      {
        fields: ['roomId', 'createdAt']
      },
      {
        fields: ['giftId']
      },
      {
        fields: ['sessionId']
      },
      {
        fields: ['userId', 'roomId', 'createdAt']
      },
      {
        fields: ['giftId', 'createdAt']
      }
    ]
  }
);

// 设置关联关系（禁用外键约束）
UserRoomGift.belongsTo(User, { foreignKey: 'userId', as: 'user', constraints: false });
UserRoomGift.belongsTo(Room, { foreignKey: 'roomId', as: 'room', constraints: false });
UserRoomGift.belongsTo(Gift, { foreignKey: 'giftId', as: 'gift', constraints: false });
UserRoomGift.belongsTo(LiveSession, { foreignKey: 'sessionId', as: 'session', constraints: false });
UserRoomGift.belongsTo(UserSnapshot, { foreignKey: 'userSnapshotId', as: 'userSnapshot', constraints: false });

User.hasMany(UserRoomGift, { foreignKey: 'userId', as: 'giftRecords', constraints: false });
Room.hasMany(UserRoomGift, { foreignKey: 'roomId', as: 'giftRecords', constraints: false });
Gift.hasMany(UserRoomGift, { foreignKey: 'giftId', as: 'giftRecords', constraints: false });
LiveSession.hasMany(UserRoomGift, { foreignKey: 'sessionId', as: 'giftRecords', constraints: false });
UserSnapshot.hasMany(UserRoomGift, { foreignKey: 'userSnapshotId', as: 'giftRecords', constraints: false });

export default UserRoomGift;